package org.example.SolutionNew003;

public class DoubleTree015 {
    public static void main(String[] args) {
        DoubleTree015 dt=new DoubleTree015();
        TreeNode root = new TreeNode(2);
        root.left = new TreeNode(-1);
//        root.right = new TreeNode(5);
//        root.right.right = new TreeNode(6);
//        root.left.right = new TreeNode(4);
//        root.left.left = new TreeNode(3);
        System.out.println(dt.maxPathSum(root));
    }

    int res=Integer.MIN_VALUE;
    public int maxPathSum(TreeNode root) {
        getMaxSum(root);
        return res;
    }

    public int getMaxSum(TreeNode root) {
        if (root == null) return 0;
        int left=getMaxSum(root.left);
        int right=getMaxSum(root.right);
        int maxLeft=left>0?left:0;
        int maxRight=right>0?right:0;
        res=Math.max(res,root.val+maxLeft+maxRight);
        return Math.max(maxLeft,maxRight)+root.val;
    }
}
